package cn.sieis.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sieis.dao.ContestMapper;
import cn.sieis.dao.SchoolMapper;
import cn.sieis.dao.SourceMapper;
import cn.sieis.dao.StatusMapper;
import cn.sieis.pojo.Contest;
import cn.sieis.pojo.Project;
import cn.sieis.pojo.ProjectVo;
import cn.sieis.pojo.School;
import cn.sieis.pojo.Source;
import cn.sieis.pojo.Status;
import cn.sieis.service.project.ProjectService;
import cn.sieis.service.school.SchoolService;

@Controller
@RequestMapping("/front/project")
public class ProjectFrontController {

	@Resource
	private ProjectService projectService;

	@Resource
	private SchoolService schoolService;
	@Autowired
	private SourceMapper sourceMapper;
	@Autowired
	private ContestMapper constestMapper;
	@Autowired
	private SchoolMapper schoolMapper;
	@Autowired
	private StatusMapper statusMapper;

	@RequestMapping(value = "/sign.html", method = RequestMethod.GET)
	public String sign(@ModelAttribute("project") Project project) {
		return "front/sign";
	}

	@RequestMapping(value = "/savesign.html", method = RequestMethod.POST)
	public String savesign(Project project, HttpSession session, HttpServletRequest request) {
		boolean flag = true;
		if (flag) {
			try {
				if (projectService.sign(project)) {
					System.out.println(1);
					return "redirect:/front/project/list.html";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "front/sign";
	}

	@RequestMapping(value = "/contestDynamics.html")
	public String contestDynamics(Model model) {
		return "front/contestDynamics";
	}

	@RequestMapping(value = "/contestDynamics2.html")
	public String contestDynamics2(Model model) {
		return "front/contestDynamics2";
	}

	@RequestMapping(value = "index.html")
	public String index(Model model) {
		return "front/index";
	}

	@RequestMapping(value = "projectlist.html")
	public String projectlist(Model model) {
		return "front/projectlist";
	}

	@RequestMapping(value = "sign.html")
	public String signF(Model model) {
		return "front/sign";
	}

	@RequestMapping(value = "/list.html")
	public String getProjectList(Model model, String proName, Integer proSchool) {
		List<Project> projectList = null;
		List<School> schoolList = null;
		try {
			projectList = projectService.getProjectListF(proName, proSchool);
			schoolList = schoolService.getSchoolList();
			// projectList=projectService.getProjectList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("projectList", projectList);
		model.addAttribute("schoolList", schoolList);
		return "front/projectlist";
	}

	@RequestMapping(value = "projectview.html", method = RequestMethod.GET)
	public String contestview(@RequestParam("id") String id, Model model, HttpServletRequest request) {

		try {
			Project project = projectService.getProjectById(Integer.valueOf(id));
			if (project != null) {
				ProjectVo vo = new ProjectVo();
				vo.setConName(project.getConName());
				final  SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-mm-dd");
				vo.setCreationDate(sdf1.format(project.getCreationDate()));
				String filePath = project.getFilePath();
				vo.setFilePath(filePath);
				if (StringUtils.isNoneBlank(filePath)) {
					Source source = sourceMapper.getSourceByFilePath(filePath);
					if (source != null) {
						vo.setFileName(source.getFilename());
					}

				}
				vo.setId(project.getId());
				vo.setProCode(project.getProCode());
				Contest contest = constestMapper.getContestById(project.getProContest());
				if (contest != null) {
					vo.setProContest(contest.getConName());
				}
				vo.setProName(project.getProName());
				List<School> schoolList = schoolMapper.getSchoolList();
				if (schoolList != null) {
					Optional<School> optional = schoolList.stream().filter(x -> x.equals(project.getId())).findFirst();
					if (optional.isPresent()) {
						vo.setProSchool(optional.get().getSchName());
					}
				}
				List<Status> statusList = statusMapper.getStatusList();

				if (statusList != null) {
					Optional<Status> optional = statusList.stream().filter(x -> x.equals(project.getProStatus())).findFirst();
					if (optional.isPresent()) {
						vo.setProStatus(optional.get().getStatus());
					}
				}

				vo.setSchName(project.getSchName());
				model.addAttribute("project", vo);

			}

		} catch (Exception e) {
			e.printStackTrace();

		}
		return "front/projectview";

	}

}
